﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Util
{
    /// <summary>
    /// 日期和时间的帮助类
    /// </summary>
    public class DateTimeHelper
    {
        /// <summary>
        /// 根据当前时间计算年龄
        /// </summary>
        /// <param name="BirthDay"></param>
        /// <returns></returns>
        public static int CalcAge(DateTime BirthDay)
        {
            return CalcAge(BirthDay, DateTime.Now);
        }
        
        /// <summary>
        /// 根据出生日期和当前日期计算年龄
        /// </summary>
        /// <param name="BirthDay"></param>
        /// <param name="CurDay"></param>
        /// <returns></returns>
        public static int CalcAge(DateTime BirthDay, DateTime CurDay)
        {
            if (BirthDay == null || CurDay == null)
                return -1;

            int age = CurDay.Year - BirthDay.Year;
            if (BirthDay > CurDay.AddYears(-age))
                age--;

            return age;
        }

        /// <summary>
        /// 计算两个时间直接相差秒数
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public static int DiffSeconds(DateTime StartTime,DateTime EndTime)
        {
            return (int)new TimeSpan(EndTime.Ticks - StartTime.Ticks).TotalSeconds;
        }

        /// <summary>
        /// 根据秒钟来返回对应的时分秒字符串
        /// </summary>
        /// <param name="seconds"></param>
        /// <param name="HourText"></param>
        /// <param name="MinuteText"></param>
        /// <param name="SecondText"></param>
        /// <returns></returns>
        public static string FormatTimeBySecond(long lSeconds,string HourText,string MinuteText,string SecondText)
        {
            string timeText = "";
            int nHours;
            int nMinutes;
            int nSeconds;

            nHours = (int)lSeconds / 3600;
            lSeconds = lSeconds - nHours * 3600;
            nMinutes = (int)lSeconds / 60;
            lSeconds = lSeconds - nMinutes * 60;
            nSeconds = (int)lSeconds;

            if (nHours > 0)
                timeText += nHours + HourText;
            if (nMinutes > 0)
                timeText += nMinutes + MinuteText;
            if (nSeconds > 0)
                timeText += nSeconds + SecondText;

            return timeText;
        }
    }
}
